﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
using UnityEngine.UI;

public class InputSystem
{

    public static bool TouchBegin()
    {
        if (Input.GetMouseButtonDown(0))
        {
            return true;
        }
        else if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began)
        {
            return true;
        }
        return false;
    }

    public static bool TouchEnd()
    {
        if (Input.GetMouseButtonUp(0))
        {
            return true;
        }
        else if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Ended)
        {
            return true;
        }
        return false;

    }

    public static bool Touching()
    {
        if (Input.GetMouseButton(0))
        {
            return true;
        }
        else if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Moved)
        {
            return true;
        }
        return false;
    }

    public static Vector2 TouchPosition()
    {
        Vector2 pos = Vector2.zero;
        if (Input.GetMouseButton(0))
        {
            pos = Input.mousePosition;
        }
        else if (Input.touchCount > 0)
        {
            pos = Input.touches[0].position;
        }
        return pos;
    }


    public static void Init()
    {
        //Input.multiTouchEnabled = true;
        beginTouch = false;
    }
    private static Vector2 mousePos = Vector2.zero;
    private static Vector2 deltaPos = Vector2.zero;
    private static Vector2 lastPos = Vector2.zero;
    private static Vector2 touchDir = Vector2.zero;
    private static Queue<Vector2> touchList = new Queue<Vector2>();
    private static bool beginTouch;
    private static void process()
    {
        if (Input.touchCount > 0)
        {
            Debug.Log(Input.touchCount);
        }

        if (InputSystem.Touching())
        {
            if (InputSystem.beginTouch == false)
            {
                InputSystem.beginTouch = true;
                InputSystem.mousePos = InputSystem.TouchPosition();
                InputSystem.lastPos = InputSystem.TouchPosition();
                InputSystem.touchList.Clear();
                InputSystem.touchList.Enqueue(InputSystem.TouchPosition());

                //GameObject.Find("Canvas/Text").GetComponent<Text>().text = "TouchBegin";
            }
            else if (InputSystem.beginTouch == true)
            {
                if (InputSystem.TouchPosition() != InputSystem.lastPos)
                {
                    if (InputSystem.touchList.Count > 2)
                    {
                        InputSystem.touchDir = InputSystem.TouchPosition() - InputSystem.touchList.Peek();
                    }
                    //InputSystem.touchDir = InputSystem.TouchPosition() - InputSystem.lastPos;

                    InputSystem.touchList.Enqueue(InputSystem.TouchPosition());
                    while (InputSystem.touchList.Count > 10)
                    {
                        InputSystem.touchList.Dequeue();
                    }
                }

                InputSystem.deltaPos = InputSystem.TouchPosition() - InputSystem.mousePos;
                InputSystem.lastPos = InputSystem.TouchPosition();

                //GameObject.Find("Canvas/Text").GetComponent<Text>().text = "MouseDrag:" + InputSystem.touchDir.normalized * 100;
            }
        }
        else
        {
            if (InputSystem.beginTouch == true)
            {
                InputSystem.beginTouch = false;
                //GameObject.Find("Canvas/Text").GetComponent<Text>().text = "TouchEnd";
            }
        }
    }

    public static void Action()
    {
        InputSystem.process();

        if (InputSystem.beginTouch)
        {
            //Debug.Log("InputSystem.touchDir.normalized * 100");
            Entity entity = GameMgr.Instance().MainEntity();
            if (entity != null)
            {
                if (entity.rotationMovement != null)
                {
                    entity.rotationMovement.turn = true;
                    entity.rotationMovement.right = InputSystem.touchDir.x > 0;
                }
                else if (entity.targetRotationMovement != null)
                {
                    entity.targetRotationMovement.turn = true;
                    int newDir = 90 - (int)(Mathf.Atan2(InputSystem.touchDir.y, InputSystem.touchDir.x) / Mathf.PI * 180);
                    while (newDir < 0)
                    {
                        newDir += 360;
                    }
                    newDir = newDir % 360;
                    if (entity.targetRotationMovement.wantRotaion != newDir)
                    {
                        entity.targetRotationMovement.wantRotaion = newDir;
                        entity.targetRotationMovement.update();

                        int r = entity.position.rotaion - entity.targetRotationMovement.wantRotaion;
                        while (r < 0)
                        {
                            r += 360;
                        }
                        r = r % 360;
                        if (r > 180)
                        {
                            entity.targetRotationMovement.right = true;
                        }
                        else
                        {
                            entity.targetRotationMovement.right = false;
                        }
                        //Debug.Log("Movement.right: " + entity.targetRotationMovement.right + "wantRotaion: " + entity.targetRotationMovement.wantRotaion);
                    }
                    //GameObject.Find("Canvas/Text").GetComponent<Text>().text = "wantRotaion: " + entity.targetRotationMovement.wantRotaion;
                }
            }
        }
        else
        {
            Entity entity = GameMgr.Instance().MainEntity();
            if (entity != null)
            {
                if (entity.rotationMovement != null)
                {
                    entity.rotationMovement.turn = false;
                }
                else if (entity.targetRotationMovement != null)
                {
                    entity.targetRotationMovement.turn = false;
                }
            }
        }
    }
}
